package com.mall.service.home;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.home.MallExtensionService;
import com.mall.custom.dao.read.MallExtensionReadMapper;
import com.mall.custom.dao.write.MallExtensionWriteMapper;
import com.mall.pojo.MallExtension;
import com.mall.pojo.MallExtensionExample;
import com.mall.pojo.MallExtensionExample.Criteria;
import com.mall.query.MallExtensionQuery;
import com.mall.query.Page;



@Service
public class MallExtensionServiceImpl implements MallExtensionService {

	@Autowired
	private MallExtensionReadMapper mallExtensionReadMapper;

	@Autowired
	private MallExtensionWriteMapper mallExtensionWriteMapper;

	@Override
	public List<MallExtension> getMallExtensionByMallExtensionQuery(MallExtensionQuery mallExtensionQuery) {
		if (mallExtensionQuery == null || mallExtensionQuery.getOnlyKey() == null || mallExtensionQuery.getOnlyKey() <= 0
				|| mallExtensionQuery.getType() == null || mallExtensionQuery.getType() < 0) {
			return null;
		}

		MallExtensionExample example = new MallExtensionExample();
		Page page = mallExtensionQuery.getPage();
		if (page == null) {
			page = Page.getInitPage();
		}
		if (StringUtils.isNotBlank(mallExtensionQuery.getOrderBy())) {
			example.setOrderByClause(mallExtensionQuery.getOrderBy());
		}
		Criteria criteria = example.createCriteria();
		if (mallExtensionQuery.getOnlyKey() != null) {
			criteria.andCommOnlyKeyEqualTo(mallExtensionQuery.getOnlyKey());
		}
		if (mallExtensionQuery.getType() != null) {
			criteria.andTypeEqualTo(mallExtensionQuery.getType());
		}
		if (mallExtensionQuery.getStatus() != null) {
			criteria.andStatusEqualTo(mallExtensionQuery.getStatus());
		}
		return mallExtensionReadMapper.selectByExample(example);
	}

	@Override
	public Integer insertMallExtension(MallExtension mallExtension) {
		mallExtension.setGmtCreate(new Date());
		mallExtension.setGmtModify(new Date());
		return this.mallExtensionWriteMapper.insertSelective(mallExtension);
	}

}
